package com.Policing.Receive;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Policing.Adapter.Input_Adapter_item;
import com.Policing.Adapter.Public_list_item_Adapter;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.Since_case.Since_case;
import com.Policing.bean.Input_item;
import com.Policing.bean.Issued_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hb.dialog.dialog.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/5/6.
 */

public class Receive_issued extends Activity {
   private TextView receivesDept,receivesUser,receivesDate,receivesRemark;
    private ListView issued_lv;
    //领用部门，领用人，申请时间，领用时间，备注
    private String receivesId;
    private TextView state_list;
    private ImageView fh;
    private String sta;
    LoadingDialog loadingDialog;
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//全部
    private List<Input_item> data = new ArrayList<>();
    private String url="http://"+ Ip_Set.IP+"/api/receivesQueryById";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receive_issued);
        inview();

    }

    private void load() {
        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("receivesId",receivesId)
                            .add("source", Since_case.set().getSource())
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222",responseData);
                    showResponse(responseData);//解析
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void showResponse(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    loadingDialog.dismiss();
                    JSONObject json = new JSONObject(responseData);
                    receivesDept.setText(json.getString("receivesDept"));
                    receivesUser.setText(json.getString("receivesUser"));
                    if (json.getString("receivesDate").equals("")){
                        receivesDate.setText(json.getString("receivesDate"));
                    }else {
                        receivesDate.setText(json.getString("receivesDate").substring(0,10));
                    }
                    receivesRemark.setText(json.getString("receivesRemark"));
                    JSONArray jsonarray = json.getJSONArray("assets");
                    if (sta.equals("已发放")){
                        Bitmap bitmap =null;
                        Gson gson = new Gson();
                        List<Issued_bean> userList = gson.fromJson(String.valueOf(jsonarray), new TypeToken<List<Issued_bean>>(){}.getType());
                        for(int i = 0;i < userList.size();i++){
                            String assetName = userList.get(i).getAssetName();
                            String deptId = userList.get(i).getDeptId();
                            String assetNumber =userList.get(i).getAssetNumber();
                            String userId = userList.get(i).getUserId();
                            String model = userList.get(i).getModel();
                            String type = userList.get(i).getPlace();
                            if (userList.get(i).getImage().equals("")){
                                /*InputStream is = getResources().openRawResource(R.mipmap.null_po);
                                bitmap = BitmapFactory.decodeStream(is);*/
                            }else {
                                byte [] input = Base64.decode(userList.get(i).getImage(), Base64.DEFAULT);
                                bitmap = BitmapFactory.decodeByteArray(input, 0, input.length);
                            }
                            data.add(new Input_item(bitmap,assetName,deptId,assetNumber,userId,model,type,""));

                        }
                        Input_Adapter_item adapter_item = new Input_Adapter_item(Receive_issued.this,R.layout.input_adapter_item,data);
                        issued_lv.setAdapter(adapter_item);
                    }else {
                        //未发放或者申请中
                        Gson gson = new Gson();
                        List<Issued_bean> userList = gson.fromJson(String.valueOf(jsonarray), new TypeToken<List<Issued_bean>>(){}.getType());
                        for(int i = 0;i < userList.size();i++){
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("odd_tv","领用单号");
                            map.put("creator_tv","数量");
                            map.put("sum_tv","种类名称");
                            map.put("state_tv","使用人");
                            map.put("scope_tv","状态");
                            map.put("Date_created_tv","使用位置");
                            map.put("Inventory_date_tv","");
                            String zt = "";
                            if (Since_case.set().getIsAudit().equals("2")){
                                zt = "通过";
                            }
                            if (Since_case.set().getIsAudit().equals("1")){
                                zt = "不通过";
                            }
                            if (Since_case.set().getIsAudit().equals("0")){
                                zt = "未审核";
                            }
                            //状态
                            map.put("scope",zt);
                            //使用人
                            map.put("state",userList.get(i).getUser());
                            //领用单号
                            map.put("odd",userList.get(i).getReceivesId());

                            map.put("Inventory_date","");
                            map.put("Date_created",userList.get(i).getPlace());

                            if (sta.equals("驳回")){
                                map.put("creator",userList.get(i).getNum());
                            }else {
                                //这里原本是注释掉的地方
                                //map.put("creator",userList.get(i).getSum());
                                map.put("creator",userList.get(i).getNum());
                            }


                            map.put("sum",userList.get(i).getType());
                            list.add(map);
                        }
                        //适配器，刚刚重写的！
                        Public_list_item_Adapter re_ad = new Public_list_item_Adapter(Receive_issued.this,list);
                        issued_lv.setAdapter(re_ad);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void inview() {
        state_list = (TextView) findViewById(R.id.state_list);
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMessage("加载中....");
        loadingDialog.show();
        fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        receivesId = intent.getStringExtra("coding_name");
         sta = intent.getStringExtra("status");
        if (sta.equals("申请中")){
            state_list.setText("申请列表");
        }
        if (sta.equals("待发放")){
            state_list.setText("待发放列表");
        }
        if (sta.equals("已发放")){
            state_list.setText("已发放列表");
        }
        if (sta.equals("驳回")){
            state_list.setText("驳回列表");
        }
        issued_lv = (ListView) findViewById(R.id.issued_lv);
        receivesDept = (TextView) findViewById(R.id.receivesDept);
        receivesUser = (TextView) findViewById(R.id.receivesUser);
        receivesDate = (TextView) findViewById(R.id.receivesDate);
        receivesRemark = (TextView) findViewById(R.id.receivesRemark);
        load();
    }
}
